import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;

/**
 * Created by yanbin on 2017/8/18 0018.
 */
public class POITest {

    public static void main(String[] args) {
        FileInputStream in = null;
        try {

            in = new FileInputStream("D:\\instance\\back1.xls");

            HSSFWorkbook workbook = new HSSFWorkbook(in);

            HSSFSheet sheet = workbook.getSheetAt(0);

            Set<String> set = new HashSet<>();

            List<List<String>> list = new ArrayList<>();
            for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
                List<String> rowList = new ArrayList<>();
                HSSFRow row = sheet.getRow(rowNum);
                System.out.println(rowNum + "  " + row.getCell(3) + "-----------------------------------------");
                String[] item = row.getCell(3).toString().split("\\+");
                System.out.println(Arrays.toString(item));
                rowList.add(row.getCell(0).toString());
                rowList.add(row.getCell(1).toString());

                for (String s : item) {
                    set.add(s);
                    rowList.add(s);
                }
                list.add(rowList);
            }
            System.out.println(set + " " + set.size());

            List<String> typeList = new ArrayList<>(set);

            HSSFSheet sheet1 = workbook.getSheetAt(1);
            sheet1.setColumnWidth(0, 15 * 256);
            sheet1.setColumnWidth(1, 15 * 256);
            HSSFRow row0 = sheet1.createRow(0);
            row0.createCell(0).setCellValue("球员的类型");
            row0.createCell(1).setCellValue("特训名称");
            Iterator<String> iterator = set.iterator();
            iterator.hasNext();
            for (int cellNum = 2; cellNum <= row0.getLastCellNum(); cellNum++ ) {
                if (iterator.hasNext()) {
                    HSSFCell cell = row0.createCell(cellNum);
                    cell.setCellValue(iterator.next());
                    sheet1.setColumnWidth(cellNum, 15 * 256);
                }
                else
                    break;
            }

            for (int rowNum = 0; rowNum < list.size(); rowNum++) {
                HSSFRow sheet1Row = sheet1.createRow(rowNum + 1);
                sheet1Row.createCell(0).setCellValue(list.get(rowNum).get(0));
                sheet1Row.createCell(1).setCellValue(list.get(rowNum).get(1));

                for (int cellNum = 2; cellNum < typeList.size() + 2; cellNum++) {
                    if (hasType(list.get(rowNum), typeList.get(cellNum - 2))) {
                        sheet1Row.createCell(cellNum).setCellValue(1);
                    } else {
                        sheet1Row.createCell(cellNum).setCellValue(0);
                    }
                }
            }
            workbook.write(new FileOutputStream("D:\\instance\\back1.xls"));


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static boolean hasType(List<String> list, String s) {


        for (int i = 0; i < list.size(); i++ ) {
            if (list.get(i).equals(s)) {
                return true;
            }
        }
        return false;
    }
}
